package com.psmz.iwi.docanalyzer.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;


public class DisabledGlassPane extends JComponent implements KeyListener {
	private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10);
	private JLabel message = new JLabel();

	public DisabledGlassPane() {

		setOpaque(false);
		Color base = UIManager.getColor("inactiveCaptionBorder");
		Color background = new Color(base.getRed(), base.getGreen(),
				base.getBlue(), 128);
		setBackground(background);
		setLayout(new GridBagLayout());



		add(message, new GridBagConstraints());
		message.setOpaque(true);
		message.setBorder(MESSAGE_BORDER);



		addMouseListener(new MouseAdapter() {
		});
		addMouseMotionListener(new MouseMotionAdapter() {
		});

		addKeyListener(this);

		setFocusTraversalKeysEnabled(false);
	}

	
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(getBackground());
		g.fillRect(0, 0, getSize().width, getSize().height);
	}

	
	@Override
	public void setBackground(Color background) {
		super.setBackground(background);

		Color messageBackground = new Color(background.getRGB());
		message.setBackground(messageBackground);
	}


	public void keyPressed(KeyEvent e) {
		e.consume();
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
		e.consume();
	}


	public void activate(String text) {
		if (text != null && text.length() > 0) {
			message.setVisible(true);
			message.setText(text);
			message.setForeground(getForeground());
		} else
			message.setVisible(false);

		setVisible(true);
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		requestFocusInWindow();
	}


	public void deactivate() {
		setCursor(null);
		setVisible(false);
	}
}